package com.fenglin.supermarketmanagement.controller

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.fenglin.supermarketmanagement.dao.UserDao
import com.fenglin.supermarketmanagement.domain.User
import com.fenglin.supermarketmanagement.service.IuserService
import com.fenglin.supermarketmanagement.utils.R
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

@RestController
@CrossOrigin("*")
@RequestMapping("/users")
class Users(
    @Autowired
    internal val userDao: UserDao,
    @Autowired
    internal val userService: IuserService
) {
    @PostMapping("login")
    @CrossOrigin("*")
    fun login(@RequestBody data: Map<String,String>): R {
        val name=data["name"]
        val paw=data["paw"]
        val qwUser = QueryWrapper<User>()
        qwUser.eq("name",name).eq("paw",paw)
        val users = userDao.selectList(qwUser)
        return if (users.size > 0){
            R(date = users[0], msg = "登录成功！")
        }else{
            R(cod = 201, msg = "账号或者密码错误")
        }
    }
    @PostMapping("add")
    @CrossOrigin("*")
    fun adduser(@RequestBody data:User): R = if(userService.save(data)){ R(date = true, msg = "添加新员工成功！") } else{ R(cod = 202,date = false, msg = "添加新员工失败！") }
    @PostMapping("upset")
    @CrossOrigin("*")
    fun upsetuser(@RequestBody data:User): R = if(userService.updateById(data)){ R(date = true, msg = "修改员工信息成功！！") } else{ R(cod = 203,date = false, msg = "修改员工信息失败！！") }
    @PostMapping("remove")
    @CrossOrigin("*")
    fun deluser(@RequestBody data:User): R = if(userService.removeById(data)){ R(date = true, msg = "删除该员工成功！") } else{ R(cod = 204,date = false, msg = "删除该员工失败！") }
    @GetMapping("all")
    @CrossOrigin("*")
    fun all(): R = R(date = userService.list(), msg = "获取成功！")
}